a = int(input())
maxik = 0
if len(str(a)) != 4 or a < 0:
    print(0)
else:
    b = a // 1000
    if (b == 2 or b == 3 or b == 5 or b == 7) and b > maxik:
        maxik = b
    b = (a // 100) % 10
    if (b == 2 or b == 3 or b == 5 or b == 7) and b > maxik:
        maxik = b
    b = (a // 10) % 10
    if (b == 2 or b == 3 or b == 5 or b == 7) and b > maxik:
        maxik = b
    b = a % 10
    if (b == 2 or b == 3 or b == 5 or b == 7) and b > maxik:
        maxik = b
    b = a // 100
    c = 0
    for i in range(2, round(b / 2) + 1):
        if b % i == 0:
            c += 1
            break
    if c == 0 and b > maxik:
        maxik = b
    b = (a // 10) % 100
    c = 0
    for i in range(2, round(b / 2) + 1):
        if b % i == 0:
            c += 1
            break
    if c == 0 and b > maxik:
        maxik = b
    b = a % 100
    c = 0
    for i in range(2, round(b / 2) + 1):
        if b % i == 0:
            c += 1
            break
    if c == 0 and b > maxik:
        maxik = b
    b = a // 10
    c = 0
    for i in range(2, round(b / 2) + 1):
        if b % i == 0:
            c += 1
            break
    if c == 0 and b > maxik:
        maxik = b
    b = a % 1000
    c = 0
    for i in range(2, round(b / 2) + 1):
        if b % i == 0:
            c += 1
            break
    if c == 0 and b > maxik:
        maxik = b
    b = a
    c = 0
    for i in range(2, round(b / 2) + 1):
        if b % i == 0:
            c += 1
            break
    if c == 0 and b > maxik:
        maxik = b
    print(maxik)